﻿using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class PlayerFire : NetworkBehaviour {
	//float fireSpeed;	//每秒射击次数
	float fireDeltaTime = 0.3f;		//两次射击间隔
	float nextFireTime;	//下一次射击的最早时间
	public GameObject bulletPre;
	GameObject conBullet;
	public GameObject spawmPoint;
	Attribute playerAttr;

	// Use this for initialization
	void Start () {
		nextFireTime = Time.time;
		playerAttr = GetComponentInParent<Attribute> ();
	}
	
	// Update is called once per frame
	void Update () {
		if (!isLocalPlayer) {
			return;
		}
		if (Input.GetButton("Fire1") && nextFireTime <= Time.time) {
			print ("fire");
			CmdFire ();
			nextFireTime = Time.time + fireDeltaTime;
			print (spawmPoint.transform.rotation);
		}
	}
	//[command]可以声明一个函数可以本客户端调用，但是会在服务端（主机）执行。且函数名必须以Cmd为前缀
	[Command]
	void CmdFire(){
		conBullet = Instantiate (bulletPre, spawmPoint.transform.position, spawmPoint.transform.rotation) as GameObject;
		conBullet.GetComponent<BulletCollision> ().getData (playerAttr.attack);
		NetworkServer.Spawn (conBullet);
	}
}
